import store from "@/store"
import api from "@/api"
import { getRegeo } from "@/utils/map"
//用户登录
function userLogin(options) {
    return new Promise(async (resolve, reject) => {
        let { member_id = "" } = options || {}
        if (!store.state.moduleUser.token) {
            await store.dispatch("userLogin", member_id)
        }
        await store.dispatch("getUserInfo")
        resolve()
    })
}
//获取位置信息
async function initLocation() {
    return new Promise(async resolve => {
        try {
            if (!uni.getStorageSync("location")) {
                let data = await getRegeo()
                store.commit("setLocation", data)
                await getAreas()
            }
            resolve()
        } catch {
            await getAreas()
            resolve()
        }
    })
}
async function getAreas() {
    return new Promise(async resolve => {
        let res = await api.getCountyByCode({ code: `${String(store.state.moduleBase.location.citycode).substr(0, 4)}00` })
        store.commit("setAreas", res)
        resolve()
    })
}
export { userLogin, initLocation }
